import processing.serial.*;
import cc.arduino.*;
Arduino arduino;

//------------------------variables---------------------------------------------//
int maxtemp = 0;
int mintemp = 0;
double temperature_front = 35;
double temperature_back = 25;
int rawtemp_a = 100;
int rawtemp_b = 100;

String fan_a_status = "OFF";
String fan_b_status = "OFF";
String fan_c_status = "OFF";
String led_a_status = "OFF";
String led_b_status = "OFF";
String led_c_status = "OFF";
String plant_type = "UNKNOWN";

Boolean manual_control = false;

BufferedReader reader;
String line;

int led_pin_a = 4;
int led_pin_b = 5;
int led_pin_c = 6;
int fan_pin_a = 11;
int fan_pin_b = 12;
int fan_pin_c = 13;
int led_pin_ok = 3;
int led_pin_bad = 2;
PFont f;
//------------------------variables-----end-------------------------------------//

void setup() {
  int sizex = screen.width*3/4;
  int sizey = screen.height*3/4;
  size(sizex, sizey);
  background(255);
  
  reader = createReader("config.txt");
  
  
  arduino = new Arduino(this, Arduino.list()[0], 57600);
  arduino.pinMode(led_pin_a, Arduino.OUTPUT);
  arduino.pinMode(led_pin_b, Arduino.OUTPUT);
  arduino.pinMode(led_pin_c, Arduino.OUTPUT);
  arduino.pinMode(led_pin_ok, Arduino.OUTPUT);
  arduino.pinMode(led_pin_bad, Arduino.OUTPUT);
  arduino.pinMode(fan_pin_a, Arduino.OUTPUT);
  arduino.pinMode(fan_pin_b, Arduino.OUTPUT);
  arduino.pinMode(fan_pin_c, Arduino.OUTPUT);
  //arduino.pinMode(4, Arduino.INPUT);
  //arduino.pinMode(5, Arduino.INPUT);
  arduino.pinMode(8, Arduino.OUTPUT);
  arduino.pinMode(7, Arduino.OUTPUT);
  
}

void draw() {
  //------------------------background----------------------------------------------//
  background(255);
  //-------------------------background---------end---------------------------------//
  
  
  //-----------------------drawing all the text-------------------------------------//
  int sizex = screen.width*3/4;
  int sizey = screen.height*3/4;
  
  f = createFont("Arial",16,true);
  try {
    line = reader.readLine();
  } catch (IOException e) {
    e.printStackTrace();
    line = null;
  }
  if (line == null) {
    // Stop reading because of an error or file is empty  
  } else {
    String[] pieces = split(line, "-");
    mintemp = int(pieces[0]);
    maxtemp = int(pieces[1]);
    plant_type = pieces[2];
  }
  
  textAlign(CENTER);
  if(maxtemp-mintemp < 5){
    textFont(f,50);
    fill(255,0,0);
    text("System Error Check config.txt file",sizex/2,sizey/2);
  }
  textFont(f,25);
  fill(0);
  text("Greenhouse Control System",sizex/2,30);
  textFont(f,15);
  text("Plant Setting: " + plant_type,sizex/2,50);
  text("Ideal Temperature Range: " + mintemp + "°C-" + maxtemp + "°C",sizex/2,70);
  text("Manual Control:" + manual_control,sizex/2,90);
  text("Temperature Information(°C)",sizex*3/4,50);
  text("Fan Status:",sizex*1/4,50);
  text("Fan A: " + fan_a_status,sizex*1/4,70);
  text("Fan B: " + fan_b_status,sizex*1/4,90);
  text("Fan C: " + fan_c_status,sizex*1/4,110);
  text("Heater Status:",sizex*1/4,150);
  text("Heater A: " + led_a_status,sizex*1/4,170);
  text("Heater B: " + led_b_status,sizex*1/4,190);
  text("Heater C: " + led_c_status,sizex*1/4,210);
  //-----------------------text--------end-----------------------------------------//
  
  
  //------------------------------temperature info---------------------------------------------//
  if((temperature_front >= mintemp) && (temperature_front <= maxtemp)){
   fill(0,255,0);
   arduino.digitalWrite(led_pin_ok, Arduino.HIGH);
   arduino.digitalWrite(led_pin_bad, Arduino.LOW);
  }else{
   fill(255,0,0); 
   arduino.digitalWrite(led_pin_ok, Arduino.LOW);
   arduino.digitalWrite(led_pin_bad, Arduino.HIGH);
  }
  text("Temperature Sensor A: " + temperature_front + "°C",sizex*3/4,70);
  if((temperature_back >= mintemp) && (temperature_back <= maxtemp)){
   fill(0,255,0);
  }else{
   fill(255,0,0); 
  }  
  text("Temperature Sensor B: " + temperature_back + "°C",sizex*3/4,90);

  //--------------------------end-------------temperature--info---------------------------------//
  
  
  //-----------------------arduino-------------control------------------------------//
  //temperature_front = arduino.analogRead(A4);
  //temperature_back = arduino.analogRead(A5);
  //cooling
  if((temperature_front >= maxtemp-2) && (temperature_back >= maxtemp-2) && (manual_control == false)){
    arduino.digitalWrite(fan_pin_a, Arduino.HIGH);
    arduino.digitalWrite(fan_pin_b, Arduino.HIGH);
    arduino.digitalWrite(fan_pin_c, Arduino.HIGH);
    arduino.digitalWrite(led_pin_a, Arduino.LOW);
    arduino.digitalWrite(led_pin_b, Arduino.LOW);
    arduino.digitalWrite(led_pin_c, Arduino.LOW);
    fan_a_status = "ON";
    fan_b_status = "ON";
    fan_c_status = "ON";
    led_a_status = "OFF";
    led_b_status = "OFF";
    led_c_status = "OFF";
    arduino.digitalWrite(led_pin_b, Arduino.LOW);
    led_b_status = "OFF";
  } 
  else if(manual_control == false) {
    if(temperature_front >= maxtemp-2){
      arduino.digitalWrite(fan_pin_a, Arduino.HIGH);
      arduino.digitalWrite(led_pin_a, Arduino.LOW);
      fan_a_status = "ON";
      led_a_status = "OFF";
      arduino.digitalWrite(fan_pin_b, Arduino.LOW);
      fan_b_status = "OFF";
      arduino.digitalWrite(led_pin_b, Arduino.LOW);
      led_b_status = "OFF";
    }
    else{
      arduino.digitalWrite(fan_pin_a, Arduino.LOW);
      fan_a_status = "OFF";
    }
    if(temperature_back >= maxtemp-2){
      arduino.digitalWrite(fan_pin_c, Arduino.HIGH);
      arduino.digitalWrite(led_pin_c, Arduino.LOW);
      fan_b_status = "ON";
      led_b_status = "OFF";
      arduino.digitalWrite(fan_pin_b, Arduino.LOW);
      fan_b_status = "OFF";
      arduino.digitalWrite(led_pin_b, Arduino.LOW);
      led_b_status = "OFF";
    }
    else{
      arduino.digitalWrite(fan_pin_c, Arduino.LOW);
      fan_c_status = "OFF";
    }
  }
  //HEATING 
  if((temperature_front <= mintemp+2) && (temperature_back <= mintemp+2) && (manual_control == false)){
    arduino.digitalWrite(fan_pin_a, Arduino.LOW);
    arduino.digitalWrite(fan_pin_b, Arduino.LOW);
    arduino.digitalWrite(fan_pin_c, Arduino.LOW);
    arduino.digitalWrite(led_pin_a, Arduino.HIGH);
    arduino.digitalWrite(led_pin_b, Arduino.HIGH);
    arduino.digitalWrite(led_pin_c, Arduino.HIGH);
    fan_a_status = "OFF";
    fan_b_status = "OFF";
    fan_c_status = "OFF";
    led_a_status = "ON";
    led_b_status = "ON";
    led_c_status = "ON";
  }
  else if(manual_control == false){
    if(temperature_front <= mintemp+2){
      arduino.digitalWrite(fan_pin_a, Arduino.LOW);
      arduino.digitalWrite(led_pin_a, Arduino.HIGH);
      fan_a_status = "OFF";
      led_a_status = "ON";
      arduino.digitalWrite(fan_pin_b, Arduino.LOW);
      fan_b_status = "OFF";
      arduino.digitalWrite(led_pin_b, Arduino.LOW);
      led_b_status = "OFF";
    }
    else{
      arduino.digitalWrite(led_pin_a, Arduino.LOW);
      led_a_status = "OFF";
    }
    if(temperature_back <= mintemp+2){
      arduino.digitalWrite(fan_pin_c, Arduino.LOW);
      arduino.digitalWrite(led_pin_c, Arduino.HIGH);
      fan_c_status = "OFF";
      led_c_status = "ON";
      arduino.digitalWrite(fan_pin_b, Arduino.LOW);
      fan_b_status = "OFF";
      arduino.digitalWrite(led_pin_b, Arduino.LOW);
      led_b_status = "OFF";
    }
    else{
      arduino.digitalWrite(led_pin_c, Arduino.LOW);
      led_c_status = "OFF";
    }
  }
  
  //---------------------arduino----------control-----end---------------------------//
  
  //--------------------constant----arduino------------------------------------------------//
  
  rawtemp_a = arduino.analogRead(5);
  rawtemp_b = arduino.analogRead(4);
  println(rawtemp_a);
  println(rawtemp_b);
  temperature_front = rawtemp_a*0.4356862745;
  temperature_back = rawtemp_b*0.404;
  //-----------------constant------arduino----end-----------------------------------------//
  if(manual_control == false){
   delay(1000); 
  }
}




//-----------------------------------------gui---------------------------input--------//
void mouseClicked(){
  if(manual_control == true){
    manual_control = false;
  }
  else if(manual_control == false){
    manual_control = true;
  }
  println(manual_control);
}

void keyTyped(){
  if(manual_control == true){
    if (key == '1') {
      if(fan_a_status == "OFF"){
        arduino.digitalWrite(fan_pin_a, Arduino.HIGH);
        fan_a_status = "ON";
      }
      else if(fan_a_status == "ON"){
        arduino.digitalWrite(fan_pin_a, Arduino.LOW);
        fan_a_status = "OFF";
      }
    } 
    else if (key == '2') {
      if(fan_b_status == "OFF"){
        arduino.digitalWrite(fan_pin_b, Arduino.HIGH);
        fan_b_status = "ON";
      }
      else if(fan_b_status == "ON"){
        arduino.digitalWrite(fan_pin_b, Arduino.LOW);
        fan_b_status = "OFF";
      }
    }
    else if (key == '3') {
      if(fan_c_status == "OFF"){
        arduino.digitalWrite(fan_pin_c, Arduino.HIGH);
        fan_c_status = "ON";
      }
      else if(fan_c_status == "ON"){
        arduino.digitalWrite(fan_pin_c, Arduino.LOW);
        fan_c_status = "OFF";
      }
    }
    else if (key == 'q') {
      if(led_a_status == "OFF"){
        arduino.digitalWrite(led_pin_a, Arduino.HIGH);
        led_a_status = "ON";
      }
      else if(led_a_status == "ON"){
        arduino.digitalWrite(led_pin_a, Arduino.LOW);
        led_a_status = "OFF";
      }
    }
    else if (key == 'w') {
      if(led_b_status == "OFF"){
        arduino.digitalWrite(led_pin_b, Arduino.HIGH);
        led_b_status = "ON";
      }
      else if(led_b_status == "ON"){
        arduino.digitalWrite(led_pin_b, Arduino.LOW);
        led_b_status = "OFF";
      }
    }
    else if (key == 'e') {
      if(led_c_status == "OFF"){
        arduino.digitalWrite(led_pin_c, Arduino.HIGH);
        led_c_status = "ON";
      }
      else if(led_c_status == "ON"){
        arduino.digitalWrite(led_pin_c, Arduino.LOW);
        led_c_status = "OFF";
      }
    }
  }
}
//-------------gui-------------------input--------end-----------------------------//


//-----------------------thermistor-formula-----------------------------------//
double Thermister(int RawADC) {
  double Temp;
  // See http://en.wikipedia.org/wiki/Thermistor for explanation of formula
  if(RawADC == 0){
   RawADC = 100; 
  }
  Temp = log(RawADC)*12.625;
  return Temp;
}
//----------------------thermistor-formula-end---------------------//
